package com.order.controller.sys;

import java.io.File;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.jfinal.upload.UploadFile;
import com.order.config.Consts;
import com.order.controller.BaseController;
import com.order.model.Dish;
import com.order.utils.DateUtils;

/** 
* @类名: DishController 
* @描述: 菜品控制器，处理查找、保存、更新、删除等操作
* @作者: 林勃 
* @时间: 2014年4月13日 下午2:29:09 
*  
*/ 
@ControllerBind(controllerKey = Consts.SYS + "dish")
public class DishController extends BaseController {

	
	/** 菜品查询方法，如果没有条件则查询所有*/
	public void getDishJson(){
		String jsonStr = "{\"NAME\":\"\",\"STATUS\":\"\",\"LAST_UPDATED_BY\":\"\",\"LAST_UPDATE_DATE\":\"\"}";
		int pagesize = (int) getParaToInt("pagesize");
		int page = (int) getParaToInt("page");
		JSONObject jsonObj = null;
		Map<String, Object> map = null;
		String condition = getPara("condition","");
		String wherejson = condition.replace("[", "").replace("]", "");	
		System.out.println("condition:"+condition);
		if (StringKit.notBlank(wherejson)) {			
			jsonObj = JSONObject.parseObject(wherejson);
			String field = jsonObj.getString("field");
			String value = jsonObj.getString("value");
			jsonStr = "{\"" + field + "\":\"" + value + "\",\"EMAIL\":\"\"}";
			jsonObj = JSONObject.parseObject(jsonStr);
		}else{
			jsonObj = JSONObject.parseObject(getPara("where", jsonStr));			
		}
		map = Dish.dao.getDishJson(page, pagesize, jsonObj);
		renderJson(map);
	}
	
	/** 
	* @Title: dishSave 
	* @描述:    保存菜品
	*/ 
	public void saveOrUpdate(){
		String jsonStr = getPara("json", "").replace("\\", "/");
		String msg = "";
		JSONObject json = JSON.parseObject(jsonStr);
		if(StringKit.isBlank(json.getString("DISH_ID"))){
			msg = Dish.dao.mySave(json);
		}else{
			msg = Dish.dao.myUpdate(json);
		}
		setAttr("msg", msg);
		renderJson();
	}
	
	/** 
	* @Title: delete 
	* @Description: 菜品删除   
	*/ 
	public void delete(){
		String msg = "";
		JSONObject json = JSONObject.parseObject(getPara("json", ""));
		msg = Dish.dao.myDelete(json);
		setAttr("msg", msg);
		renderJson();
	}
	
	/** 
	* @Title: uploadImg 
	* @描述:    上传图片
	*/ 
	public void uploadImg(){
		//
		String msg = "";
		String image = "";
		String requestPath = getRequest().getRealPath("/");
		String saveDirectory = requestPath + "\\upload\\temp\\";
		try{
			//UploadFile file = getFile("IMAGE", saveDirectory, Consts.MAX_POST_SIZE, "UTF-8");
			UploadFile file = getFile();
			if(file == null){
				System.out.println("file is null");
			}else{
				//更改路径+重命名
				System.out.println(file.getSaveDirectory());
				System.out.println(file.getFileName());
				System.out.println(file.getContentType());
				
				File f = new File(saveDirectory);
				if(!f.exists()){
					f.mkdirs();
				}
				String oldName = file.getFileName();
				String newName = oldName.substring(oldName.lastIndexOf('.'));
				newName = saveDirectory + DateUtils.getNowTime("yyyyMMdd_HHmmss") + newName;
				System.out.println(newName);
				f = new File(newName);
				file.getFile().renameTo(f);
				msg = "success";
				image = newName.replace(requestPath, "");
			}
		}catch(Exception e){
			msg = "fail";
			e.printStackTrace();
		}
		JSONObject json = new JSONObject();
		json.put("msg", msg);
		json.put("image", image);
		System.out.println(json);
		renderText(json.toJSONString());
	}
}
